package com.zwk.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwk.common.utils.PageUtils;
import com.zwk.common.utils.Query;
import com.zwk.common.constant.ProductConstant;
import com.zwk.gulimall.product.dao.AttrGroupDao;
import com.zwk.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zwk.gulimall.product.entity.AttrEntity;
import com.zwk.gulimall.product.entity.AttrGroupEntity;
import com.zwk.gulimall.product.service.AttrAttrgroupRelationService;
import com.zwk.gulimall.product.service.AttrGroupService;
import com.zwk.gulimall.product.service.AttrService;
import com.zwk.gulimall.product.service.CategoryService;
import com.zwk.gulimall.product.vo.GroupWithAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<AttrGroupEntity> query = new LambdaQueryWrapper<>();
        query.and((obj) -> {
            Object key = params.get("key");
            obj.eq(AttrGroupEntity::getAttrGroupId, key).or().like(AttrGroupEntity::getAttrGroupName, key);
        });

        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                query
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        if (categoryId == 0) {
            return queryPage(params);
        }
        IPage<AttrGroupEntity> page = new Query<AttrGroupEntity>().getPage(params);
        LambdaQueryWrapper<AttrGroupEntity> query = new LambdaQueryWrapper<>();

        query.eq(AttrGroupEntity::getCatelogId, categoryId);
        //obj:wrapper对象
        if (params.containsKey("key")) {
            query.and((obj) -> {
                Object key = params.get("key");
                obj.eq(AttrGroupEntity::getAttrGroupId, key).or().like(AttrGroupEntity::getAttrGroupName, key);
            });
        }
        return new PageUtils(this.page(page, query));
    }

    /**
     * 查询分类关联的属性
     *
     * @param attrgroupId
     * @return
     */
    @Override
    public List<AttrEntity> getRelationAttr(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> list = relationService.
                list(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId));
        if (list == null || list.size() <= 0) {
            return null;
        }
        List<Long> attrIds = list.stream().map(attr -> attr.getAttrId()).collect(Collectors.toList());
        return attrService.listByIds(attrIds);
    }

    /**
     * 查询此分租没有关联的属性
     *
     * @param attrgroupId
     * @return
     */
    @Override
    public PageUtils getNoRelationAttr(Long attrgroupId, Map<String, Object> params) {

        AttrGroupEntity attrGroupEntity = this.getById(attrgroupId);
        List<AttrAttrgroupRelationEntity> list = this.relationService.list();
        //所有分组关联的attr
        List<Long> attrIds = list.stream().map(relation -> relation.getAttrId()).collect(Collectors.toList());
        LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<>();
        String key= (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.and(w->{
                w.eq(AttrEntity::getAttrId,key).or().like(AttrEntity::getAttrName,key);
            });
        }
        wrapper.eq(AttrEntity::getCatelogId, attrGroupEntity.getCatelogId())
                .ne(AttrEntity::getAttrType, ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode())
                .notIn(AttrEntity::getAttrId, attrIds);
        IPage<AttrEntity> resPage = attrService.page(new Query<AttrEntity>().getPage(params), wrapper);
        return new PageUtils(resPage);
    }

    @Override
    public boolean removeRelation(List<AttrAttrgroupRelationEntity> relationEntity) {
        if (relationEntity==null||relationEntity.size()<=0){
            return false;
        }
        for (AttrAttrgroupRelationEntity relation : relationEntity) {
            relationService.remove(
                    new LambdaUpdateWrapper<AttrAttrgroupRelationEntity>()
                            .eq(AttrAttrgroupRelationEntity::getAttrId,relation.getAttrId())
                            .eq(AttrAttrgroupRelationEntity::getAttrGroupId,relation.getAttrGroupId())
            );
        }
        return true;
    }

    @Override
    public List<GroupWithAttrVo> groupWithAttrByCatId(Long catelogId) {
        List<AttrGroupEntity> groupList = this.list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId));
        List<GroupWithAttrVo> vos = groupList.stream().map(group -> {
            GroupWithAttrVo vo = new GroupWithAttrVo();
            BeanUtils.copyProperties(group, vo);
            List<AttrEntity> list = this.getRelationAttr(group.getAttrGroupId());
            vo.setAttrs(list);
            return vo;
        }).collect(Collectors.toList());
        return vos;
    }

}